package com.sailfish.springbootdemo.controller.db1;

import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.Upkeep;
import com.sailfish.springbootdemo.pojo.db1.UpkeepAudit;
import com.sailfish.springbootdemo.service.db1.UpkeepAuditService;
import com.sailfish.springbootdemo.service.db1.UpkeepService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/upkeepAudit")
public class UpkeepAuditController {

    @Autowired
    private UpkeepAuditService upkeepAuditService;

    @PostMapping("/addUpkeepAudit")
    public Result addUpkeepAudit(UpkeepAudit upkeepAudit) {
        return upkeepAuditService.addUpkeepAudit(upkeepAudit);
    }

    @PostMapping("/deleteUpkeepAudit")
    public Result deleteUpkeepAudit(Integer upkeepId) {
        return upkeepAuditService.deleteUpkeepAudit(upkeepId);
    }

    @PostMapping("/updateUpkeepAudit")
    public Result updateUpkeepAudit(UpkeepAudit upkeepAudit) {
        try {
            if (upkeepAudit.getUpkeepId() == null || "".equals(upkeepAudit.getUpkeepId().toString())) {
                return ResultUtil.error(500, "缺少参数upkeepId", "Miss Param upkeepId", null);
            }
            Boolean b = upkeepAuditService.updateUpkeepAudit(upkeepAudit);
            if (b) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @PostMapping("/getUpkeepAuditByAssetNumber")
    public Result getUpkeepAuditByAssetNumber(String assetNumber, String isDeleteStr) {
        try {
            if (assetNumber == null || "".equals(assetNumber)) {
                return ResultUtil.error(500, "缺少参数assetNumber", "Miss Param assetNumber", null);
            }
            return ResultUtil.success(upkeepAuditService.getUpkeepAuditByAssetNumber(assetNumber, isDeleteStr));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @PostMapping("/auditUpkeepAudit")
    public Result auditUpkeepAudit(String notes, Integer upkeepId, Integer upkeepStatus) {
        return upkeepAuditService.auditUpkeepAudit(notes, upkeepId, upkeepStatus);
    }

    @PostMapping("/getById")
    public Result getById(Integer upkeepId) {
        try {
            if (upkeepId == null || "".equals(upkeepId.toString())) {
                return ResultUtil.error(500, "缺少参数upkeepId", "Miss Param upkeepId", null);
            }
            return ResultUtil.success(upkeepAuditService.getById(upkeepId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
